package com.polaris.live.common.photo.image_preview

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.os.Build
import android.view.View
import java.lang.ref.WeakReference

/**
 * 图片预览
 *
 * @author Created by 半仙 on 2023/8/11/011 17:33
 */
class ImagePreview {

    private var contextWeakReference: WeakReference<Context> = WeakReference(null)

    private var imageInfoList = listOf<String>()

    // 防止多次快速点击，记录上次打开的时间戳
    private var lastClickTime: Long = 0

    // 用于过度动画的 VIEW
    var transitionView: View? = null
        private set

    // 过度动画的共享元素名
    var transitionShareElementName: String? = null
        private set

    fun setContext(context: Context): ImagePreview {
        contextWeakReference = WeakReference(context)
        return this
    }

    //默认展示第几个
    private var index = 0

    /**
     * 设置用于过度动画的共享元素view<br></br>
     * 需要同时设置 [ImagePreview.setTransitionShareElementName]
     */
    fun setTransitionView(transitionView: View?): ImagePreview {
        this.transitionView = transitionView
        return this
    }

    /**
     * 设置用于过度动画的共享元素name <br></br>
     * 需要同时设置 [ImagePreview.setTransitionView]
     */
    fun setTransitionShareElementName(transitionShareElementName: String?): ImagePreview {
        this.transitionShareElementName = transitionShareElementName
        return this
    }

    fun setIndex(index: Int): ImagePreview {
        this.index = index
        return this
    }

    fun setImageInfoList(imagInfoList: List<String>): ImagePreview {
        this.imageInfoList = imagInfoList
        return this
    }

    fun getImageInfoList(): List<String> {
        return imageInfoList
    }

    private fun reset() {
        transitionView = null
        transitionShareElementName = null
        contextWeakReference.clear()
        index = 0
    }

    fun start() {
        if (System.currentTimeMillis() - lastClickTime <= MIN_DOUBLE_CLICK_TIME) {
            return
        }
        val context =
            contextWeakReference.get() ?: throw IllegalAccessException("你得setContext啊老铁")
        require(context is Activity) { "就有没有可能，你得传Activity。。" }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (context.isFinishing || context.isDestroyed) {
                reset()
                return
            }
        } else {
            if (context.isFinishing) {
                reset()
                return
            }
        }
        require(imageInfoList.isNotEmpty()) { "Do you forget to call 'setImageInfoList(List<ImageInfo> imageInfoList)' ?" }
        require(index < imageInfoList.size) { "咱就说这个下标，你想看哪张图" }
        lastClickTime = System.currentTimeMillis()
        ImagePreviewActivity.activityStart(context)
    }

    /**
     * 主动关闭页面
     */
    fun finish() {
        val context = contextWeakReference.get()
        if (context !is Activity) {
            return
        }
        if (context.isFinishing || context.isDestroyed) {
            return
        }
        context.finish()
    }

    private object InnerClass {

        @SuppressLint("StaticFieldLeak")
        val instance = ImagePreview()
    }

    companion object {

        // 触发双击的最短时间，小于这个时间的直接返回
        private const val MIN_DOUBLE_CLICK_TIME = 1500

        @JvmStatic
        val instance: ImagePreview
            get() = InnerClass.instance
    }
}